#!/usr/bin/python
# encoding: utf-8
# -*- coding: utf8 -*-
import os
import sys
#from files import *

class  File:
    def read(self,Filename):
        with open(Filename, 'r') as Mes:
            return Mes.readlines()
    def add(self,Filename,addMes):
        with open(Filename,'a+') as Mes:
            Mes.write(addMes)
    def write(self,Filename,addMes):
        with open(Filename,'w') as Mes:
            Mes.write(addMes)



class OsFormat:
    def __init__(self):
        self.LOGNAME="/tmp/OsFormat.log"
        self.HOSTNAMEFILE="/etc/hostname"
        try:
            import netifaces
        except ImportError:
            try:
                command_to_execute = "pip install netifaces -i http://172.25.0.110/pypi/web/simple --trusted-host 172.25.0.110"
                os.system(command_to_execute)
            except OSError:
                print "Can NOT install netifaces, Aborted!"
                sys.exit(1)
            import netifaces
        self.DefaultNicName = netifaces.gateways()['default'][netifaces.AF_INET][1]
        self.IPAddr = netifaces.ifaddresses(self.DefaultNicName)[netifaces.AF_INET][0]['addr']
        #print "NicName:%s\t\tIPAddr:%s\n" %(self.DefaultNicName,self.IPAddr)
    def getLocalIPAddr(self):
        return self.IPAddr
    def formatNetwork(self,ipaddress,subnet,gateway):
        defaultNicPath = "/etc/sysconfig/network-scripts/ifcfg-%s" %(self.DefaultNicName)
        defaultNicMode='''NAME=%s
DEVICE=%s
IPV6INIT=no
BOOTPROTO=none
ONBOOT=yes
TYPE=Ethernet
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
DNS1=172.31.0.1
DNS2=172.31.0.2
DOMAIN=tendcloud.com
IPADDR=%s
PREFIX=%s
GATEWAY=%s
''' % (self.DefaultNicName,self.DefaultNicName,ipaddress,subnet,gateway)
        netifacesInfo="Active NIC Name:%s\nNicPath:%s\nNicInfo:%s\n" % (self.DefaultNicName,defaultNicPath,defaultNicMode)
        File().write(defaultNicPath,defaultNicMode)
        File().add(self.LOGNAME,netifacesInfo)
    def formatHostname(self,hostname):
        File().write(self.HOSTNAMEFILE,hostname)
        hostnamemsg= "HostName : %s\t\tFILE : %s\n" %(hostname,self.HOSTNAMEFILE)
        #print hostnamemsg
        
class main:
    home='/tmp/OsFormat'
    nodeListFile='%s/node.list' % home
    nodeLists = File().read(nodeListFile)
    restart='/usr/sbin/init 6'
    for node in nodeLists:
        localIpAddr = OsFormat().getLocalIPAddr()
        if localIpAddr == node.split()[0]:
            #print "localIpAddr:%s\nnode : %s" %(localIpAddr,node.split())
            dhcpIp=node.split('\t')[0].strip()
            ipaddress=node.split('\t')[1].strip()
            hostName=node.split('\t')[2].strip()
            subnet=node.split('\t')[3].strip()
            gateway=node.split('\t')[4].split('\n')[0]
            nodeline = "dhcpip:%s\tipadd:%s\thostname:%s\tsubnet:%s\tgateway:%s\n" %(dhcpIp,ipaddress,hostName,subnet,gateway)
            OsFormat().formatNetwork(ipaddress,subnet,gateway)
            OsFormat().formatHostname(hostName)
            os.system(restart)
if __name__ == '__main__':
    main()
